from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.action_chains import ActionChains
# 1. 手动指定 ChromeDriver 路径
from selenium.webdriver.common.by import By

# 行为链
service = Service('E:/devtools/Python/Python312/chromedriver.exe')  # 替换为实际路径
chrome = webdriver.Chrome(service=service)

try:
    chrome.get("https://cn.bing.com/")
    input_tag = chrome.find_element(By.ID, 'sb_form_q')
    search = chrome.find_element(By.ID, 'sb_form_go')

    actitions = ActionChains(chrome)
    # 移动到输入框上
    actitions.move_to_element(input_tag)
    # 在文本中输入关键字
    actitions.send_keys_to_element(input_tag, 'Python')
    # 移动到按钮
    actitions.move_to_element(search)
    # 点击按钮
    actitions.click(search)
    # 执行行为链
    actitions.perform()
    print("执行完成...")
    input("回车键关闭浏览器...")
except Exception as e:
    print(f"执行过程中出错: {e}")
finally:
    chrome.quit()
